﻿using System.Linq;
using mediaPrime.Sdk;

namespace mediaPrime.Plugin.TV.UserControls
{
    /// <summary>
    /// Interaktionslogik für TVServerProviderSettingsDialog.xaml
    /// </summary>
    public partial class TVServerProviderSettingsDialog : CustomSettingsDialog
    {
        private TVPlugin tvPlugin;

        public TVServerProviderSettingsDialog(TVPlugin tvPlugin)
        {
            InitializeComponent();

            this.tvPlugin = tvPlugin;

            listBox.ItemsSource = tvPlugin.TVServerProviders;
        }

        public override void Load(Setting setting)
        {
            // TODO: What if no tv server providers are available
            if (!string.IsNullOrEmpty(setting.Value))
                listBox.SelectedItem = tvPlugin.TVServerProviders.Where(t => t.Id == setting.Value).FirstOrDefault();
        }

        public override void Save(Setting setting)
        {
            if (listBox.SelectedItem is ExtensionInfo)
                setting.SetValue((listBox.SelectedItem as ExtensionInfo).Id);
        }

        private void listBox_ItemClicked(object sender, Controls.MPMenuItemClickedEventArgs e)
        {
            DialogResult = true;
        }
    }
}
